<?php

class SitemapGenerator
{
    public function Generate( TemplateEngine $tmpl, $pageInfo, $depth )
    {
        if ( empty($pageInfo) || !$pageInfo->sitemap )
        {
            return '';
        }

        $childContent = '';

        foreach ( $pageInfo->children as $childId )
        {
            $childInfo = Pages::GetPageInfo($childId);
            $childContent .= $this->Generate( $tmpl, $childInfo, $depth + 1 );
        }

        $tmpl->clearAll();
        $tmpl->define("header0", "sitemap_header0.tpl");
        $tmpl->define("header1", "sitemap_header1.tpl");
        $tmpl->define("header2", "sitemap_header2.tpl");
        $tmpl->define("link",    "sitemap_link.tpl");

        $tmpl->set('LINK', $pageInfo->link ? UrlHelper::GetAbsolutePath($pageInfo->path) : '');
        $tmpl->set('TEXT', $pageInfo->name);
        $tmpl->set('CHILDS', $childContent);

        if ( $depth == 0 )
        {
             $tmpl->parse("result", "header0");
        }
        else if ( !empty($childContent) )
        {
            $tmpl->parse("result", ($depth == 1) ? "header1" : "header2");
        }
        else
        {
            $tmpl->parse("result", "link");
        }

        return $tmpl->getGenerated();
        
    }
}

?>
